package com.example.demo.resp;

import com.example.demo.exception.AppExceptionCodeMsg;
import lombok.Data;
import lombok.experimental.Accessors;
import java.text.SimpleDateFormat;
import java.util.Date;

/**

import com.example.demo.exception.AppExceptionCodeMsg;
import lombok.Data;
import lombok.experimental.Accessors;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @projectName: demo1
 * @package: com.example.demo.tool.response
 * @className: Resp
 * @author: Eric
 * @description: TODO
 * @date: 2025/1/17 15:40
 * @version: 1.0
 */
@Data
@Accessors(chain = true)
public class ResultData<T> {

    private Integer code=200;
    private String message="success";
    private T data;
    private String timestamp;

    public ResultData(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        long timestamp = System.currentTimeMillis();
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd_HH:mm:ss:SSS");
        this.timestamp = sdf.format(date);
    }


    public static <T> ResultData<T> success(T data) {
        return new ResultData<>(200, "success", data);
    }

    public static <T> ResultData<T> success(String msg, T data) {
        return new ResultData<>(200, msg, data);
    }

    //    error
    public static <T> ResultData<T> error(AppExceptionCodeMsg appExceptionCodeMsg){
        return new ResultData<>(
                appExceptionCodeMsg.getCode(),
                appExceptionCodeMsg.getMsg(),
                null
        );
    }
    public static <T> ResultData<T> error(Integer code,String msg) {
        return new ResultData<>(code, msg, null);
    }
}